**********************************************************************************************
* Replication file: "Low skill products by high skill workers. The distributive effects of trade in emerging and developing countries"
* Irene Menéndez González, Erica Owen and Stefanie Walter. December 2022
**********************************************************************************************

**********************************************************************************************
* Main analysis: macro-level
**********************************************************************************************

clear
set more off

capture log close

use DataIneqTrade_ready.dta, clear


* DV: market and disposable income inequality
***********************************************************************
* mgini_mk: measure of market income inequality containing the row mean of the inequality estimates
 
* mgini_dp: measure of disposable income inequality containing the row mean of the inequality estimates


* Main IVs
***********************************************************************

* Trade volume: imports + exports as % GDP
gen tradef = trade/100

* Exports in manufacturing (% merchandise)
gen manufexpf = manufexp/100

* Imports in manufacturing (% merchandise)
gen manufimpf = manufimp/100

* Exports in agriculture (% of merchandise) 
gen agrexpf = agrexp/100

* Imports in agriculture (% of merchandise)
gen agrimpf = agrimp/100

* Exports in food (% of merchandise) 
gen foodexpf = foodexp/100

* Imports in food (% of merchandise)
gen foodimpf = foodimp/100

* Exports in fuels (% of merchandise) 
gen fuelexpf=fuelexp/100

* Imports in fuels (% of merchandise) 
gen fuelimpf=fuelimp/100

* Sum of commodities
gen comtotalx=agrexpf + foodexpf + fuelexpf
gen comtotalm=agrimpf + foodimpf + fuelimpf


* Polity indicator
gen demo=.
replace demo=1 if polity>=6
replace demo=0 if polity<6
lab def de 0 "Non-democracy" 1 "Democracy"
lab val demo de
lab var demo "Democracy"

* Ideology
gen ideo=.
replace ideo=1 if execrlc==3
replace ideo=0 if execrlc==2
replace ideo=0 if execrlc==1

lab def id 0 "Center/Right" 1 "Left"
lab val ideo id
lab var ideo "Ideology"

* Proportional representation - pr (3 categories) 
recode pr (1=2) (0=1) (-999=0)
lab def r 0 "NA" 1 "Plurality" 2 "PR"
lab val pr r
lab var pr "Proportional representation"

* Capita GDP (log) 
gen log_gdp=log(gdpcap)

* Capita GDP squared
gen gdpsq=log_gdp^2

* Population growth (log)
gen log_pop=log(pop)

* Growth gdp (log): annual % growth rate GDP
gen log_grthgdp=log(grthgdp)


xtset acode year 

* Lags

gen manufexpfL1=L1.manufexpf

gen manufimpfL1=L1.manufimpf

gen foodexpfL1=L1.foodexpf

gen foodimpfL1=L1.foodimpf

gen agrexpfL1=L1.agrexpf

gen agrimpfL1=L1.agrimpf

gen fuelexpfL1=L1.fuelexpf

gen fuelimpfL1=L1.fuelimpf

gen comtotalxL1=L1.comtotalx

gen comtotalmL1=L1.comtotalm

gen popL1=L1.pop

gen log_gdpL1=L1.log_gdp

gen gdpsqL1=L1.gdpsq

gen tradefL1=L1.tradef

gen grthgdpL1=L1.grthgdp

gen prL1=L1.pr

gen ideoL1=L1.ideo

gen demoL1=L1.demo

* Period dummies
gen period=.

replace period=0 if year==1960
replace period=0 if year==1961
replace period=0 if year==1962
replace period=0 if year==1963
replace period=0 if year==1964
replace period=0 if year==1965
replace period=0 if year==1966
replace period=0 if year==1967
replace period=0 if year==1968
replace period=0 if year==1969
replace period=1 if year==1970
replace period=1 if year==1971
replace period=1 if year==1972
replace period=1 if year==1973
replace period=1 if year==1974
replace period=1 if year==1975
replace period=1 if year==1976
replace period=1 if year==1977
replace period=1 if year==1978
replace period=1 if year==1979
replace period=2 if year==1980
replace period=2 if year==1981
replace period=2 if year==1982
replace period=2 if year==1983
replace period=2 if year==1984
replace period=2 if year==1985
replace period=2 if year==1986
replace period=2 if year==1987
replace period=2 if year==1988
replace period=2 if year==1989
replace period=3 if year==1990
replace period=3 if year==1991
replace period=3 if year==1992
replace period=3 if year==1993
replace period=3 if year==1994
replace period=3 if year==1995
replace period=3 if year==1996
replace period=3 if year==1997
replace period=3 if year==1998
replace period=3 if year==1999
replace period=4 if year==2000
replace period=4 if year==2001
replace period=4 if year==2002
replace period=4 if year==2003
replace period=4 if year==2004
replace period=4 if year==2005
replace period=4 if year==2006
replace period=4 if year==2007
replace period=4 if year==2008
replace period=4 if year==2009
replace period=5 if year==2010
replace period=5 if year==2011
replace period=5 if year==2012
replace period=5 if year==2013
replace period=5 if year==2014
replace period=5 if year==2015
replace period=5 if year==2016

xtset acode year 

* Regression models
***********************************************************************

* Table 3: Effect of trade exposure on market income inequality in developing countries, 1960-2016

* Model 1. With manufactured exports and imports
xtreg mgini_mk manufexpfL1 manufimpfL1 demoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 

* Model 2. With trade volume
xtreg mgini_mk tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 

* Model 3. Controlling for trade volume
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 

* Model 4. With period effects
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 

* Model 5. Controlling for sum of commodities exports
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 comtotalxL1 comtotalmL1 demoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) // baseline model

* Model 6. Controlling for sum of commodities exports and period fixed effects
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 comtotalxL1 comtotalmL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 


* Table 4: Effect of trade exposure on disposable income inequality in developing countries, 1960-2016

* Model 1. With manufactured exports and imports
xtreg mgini_dp manufexpfL1 manufimpfL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 

* Model 2. With trade volume
xtreg mgini_dp tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 

* Model 3. Controlling for trade volume
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode)

* Model 4. With period effects
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 

* Model 5. Controlling for sum commodities exports
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 comtotalxL1 comtotalmL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 

* Model 6. Controlling for sum commodities exports and period fixed effects
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 comtotalxL1 comtotalmL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 


* Exporting results
***********************************************************************

* Table 3: Effect of trade exposure on market income inequality in emerging markets and developing countries, 1960-2016

* Model 1. With manufactured exports and imports
xtreg mgini_mk manufexpfL1 manufimpfL1 demoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 
outreg2 using ldcmacro.doc, replace ctitle(Model 1) dec(4) label addtext(Country FE, Yes) 

* Model 2. With trade volume
xtreg mgini_mk tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 
outreg2 using ldcmacro.doc, append ctitle(Model 2) dec(4) label addtext(Country FE, Yes)

* Model 3. Controlling for trade volume
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 
outreg2 using ldcmacro.doc, append ctitle(Model 3) dec(4) label addtext(Country FE, Yes)

* Model 4. With period effects
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro.doc, append ctitle(Model 4) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Model 5. Controlling for sum of commodities exports
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 comtotalxL1 comtotalmL1 demoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) // baseline model
outreg2 using ldcmacro.doc, append ctitle(Model 5) dec(4) label addtext(Country FE, Yes)

* Model 6. Controlling for sum of commodities exports and period fixed effects
xtreg mgini_mk manufexpfL1 manufimpfL1 tradefL1 comtotalxL1 comtotalmL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro.doc, append ctitle(Model 6) dec(4) label addtext(Country FE, Yes, Period FE, Yes)


* Table 4: Effect of trade exposure on disposable income inequality in emerging markets and developing countries, 1960-2016

* Model 1. With manufactured exports and imports
xtreg mgini_dp manufexpfL1 manufimpfL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 
outreg2 using ldcmacro1.doc, replace ctitle(Model 1) dec(4) label addtext(Country FE, Yes) 

* Model 2. With trade volume
xtreg mgini_dp tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 
outreg2 using ldcmacro1.doc, append ctitle(Model 2) dec(4) label addtext(Country FE, Yes)

* Model 3. Controlling for trade volume
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode)
outreg2 using ldcmacro1.doc, append ctitle(Model 3) dec(4) label addtext(Country FE, Yes)

* Model 4. With period effects
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro1.doc, append ctitle(Model 4) dec(4) label addtext(Country FE, Yes, Period FE, Yes)

* Model 5. Controlling for sum commodities exports
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 comtotalxL1 comtotalmL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 if region==1, fe cluster(acode) 
outreg2 using ldcmacro1.doc, append ctitle(Model 5) dec(4) label addtext(Country FE, Yes)

* Model 6. Controlling for sum commodities exports and period fixed effects
xtreg mgini_dp manufexpfL1 manufimpfL1 tradefL1 comtotalxL1 comtotalmL1 demoL1 ib1.prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.period if region==1, fe cluster(acode) 
outreg2 using ldcmacro1.doc, append ctitle(Model 6) dec(4) label addtext(Country FE, Yes, Period FE, Yes)



* Figure 5 (make sure to run at end of do-file)
***********************************************************************

* Relationship between manufactured exports and market and disposable income inequality in emerging and developing economies
* Based on model 3, Tables 3 & 4
set scheme lean2
drop if mgini_mk==. // make sure to predict residuals for those countries for which there is data on inequality

qui reg mgini_mk manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.acode 
predict resginimk, resid

qui reg manufexpfL1 manufimpfL1 tradefL1 demoL1 log_gdpL1 grthgdpL1 popL1 i.acode  
predict resmanufexpL1, resid

scatter resginimk resmanufexpL1 if region==1, graphregion(color(white)) title("Market income inequality, 1960-2015") ytitle("Market income inequality") xtitle("Manufactured exports") ylabel(, nogrid) msize(tiny) legend(off) saving(left, replace) || lfit resginimk resmanufexpL1 if region==1

reg resginimk resmanufexpL1 if region==1

qui reg mgini_dp manufimpfL1 tradefL1 demoL1 prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.acode 
predict resginidp, resid

qui reg manufexpfL1 manufimpfL1 tradefL1 demoL1 prL1 ideoL1 log_gdpL1 grthgdpL1 popL1 i.acode  
predict resmanufexpL1_, resid

scatter resginidp resmanufexpL1_ if region==1, graphregion(color(white)) title("Disposable income inequality, 1960-2015") ytitle("Disposable income inequality") xtitle("Manufactured exports") ylabel(, nogrid) msize(tiny) legend(off) saving(right, replace) || lfit resginidp resmanufexpL1_ if region==1

gr combine left.gph right.gph, ycommon col(2) name(cg, replace) 

*****************************************************************************************



